'use strict';

const Controller = require('egg').Controller;

class QuestionAnswerController extends Controller {
  // 根据题目ID获取答案
  async index() {
    const { ctx } = this;
    const { question_id } = ctx.query;
    try {
      if (!question_id) {
        ctx.body = {
          code: 400,
          message: '题目ID不能为空',
          data: null,
        };
        return;
      }
      const answers = await ctx.service.questionAnswer.findByQuestionId(question_id);
      ctx.body = {
        code: 200,
        message: '获取答案成功',
        data: answers,
      };
    } catch (error) {
      ctx.logger.error('获取答案失败:', error);
      ctx.body = {
        code: 500,
        message: '获取答案失败',
        data: null,
      };
    }
  }

  // 根据ID获取答案
  async show() {
    const { ctx } = this;
    const { id } = ctx.params;
    try {
      const answer = await ctx.service.questionAnswer.findById(id);
      if (answer) {
        ctx.body = {
          code: 200,
          message: '获取答案详情成功',
          data: answer,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '答案不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('获取答案详情失败:', error);
      ctx.body = {
        code: 500,
        message: '获取答案详情失败',
        data: null,
      };
    }
  }

  // 创建答案
  async create() {
    const { ctx } = this;
    const { question_id, answer_content } = ctx.request.body;
    try {
      if (!question_id || !answer_content) {
        ctx.body = {
          code: 400,
          message: '题目ID和答案内容不能为空',
          data: null,
        };
        return;
      }
      const answer = await ctx.service.questionAnswer.create({ question_id, answer_content });
      ctx.body = {
        code: 200,
        message: '创建答案成功',
        data: answer,
      };
    } catch (error) {
      ctx.logger.error('创建答案失败:', error);
      ctx.body = {
        code: 500,
        message: '创建答案失败',
        data: null,
      };
    }
  }

  // 更新答案
  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { answer_content } = ctx.request.body;
    try {
      if (!answer_content) {
        ctx.body = {
          code: 400,
          message: '答案内容不能为空',
          data: null,
        };
        return;
      }
      const answer = await ctx.service.questionAnswer.update(id, { answer_content });
      if (answer) {
        ctx.body = {
          code: 200,
          message: '更新答案成功',
          data: answer,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '答案不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('更新答案失败:', error);
      ctx.body = {
        code: 500,
        message: '更新答案失败',
        data: null,
      };
    }
  }

  // 删除答案
  async destroy() {
    const { ctx } = this;
    const { id } = ctx.params;
    try {
      const result = await ctx.service.questionAnswer.delete(id);
      if (result) {
        ctx.body = {
          code: 200,
          message: '删除答案成功',
          data: null,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '答案不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('删除答案失败:', error);
      ctx.body = {
        code: 500,
        message: '删除答案失败',
        data: null,
      };
    }
  }
}

module.exports = QuestionAnswerController;
